package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.NoteLikeMapper;
import com.ruoyi.system.domain.NoteLike;
import com.ruoyi.system.service.INoteLikeService;

/**
 * 笔记点赞记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class NoteLikeServiceImpl implements INoteLikeService 
{
    @Autowired
    private NoteLikeMapper noteLikeMapper;

    /**
     * 查询笔记点赞记录
     * 
     * @param likeId 笔记点赞记录主键
     * @return 笔记点赞记录
     */
    @Override
    public NoteLike selectNoteLikeByLikeId(Long likeId)
    {
        return noteLikeMapper.selectNoteLikeByLikeId(likeId);
    }

    /**
     * 查询笔记点赞记录列表
     * 
     * @param noteLike 笔记点赞记录
     * @return 笔记点赞记录
     */
    @Override
    public List<NoteLike> selectNoteLikeList(NoteLike noteLike)
    {
        return noteLikeMapper.selectNoteLikeList(noteLike);
    }

    /**
     * 新增笔记点赞记录
     * 
     * @param noteLike 笔记点赞记录
     * @return 结果
     */
    @Override
    public int insertNoteLike(NoteLike noteLike)
    {
        noteLike.setCreateTime(DateUtils.getNowDate());
        return noteLikeMapper.insertNoteLike(noteLike);
    }

    /**
     * 修改笔记点赞记录
     * 
     * @param noteLike 笔记点赞记录
     * @return 结果
     */
    @Override
    public int updateNoteLike(NoteLike noteLike)
    {
        return noteLikeMapper.updateNoteLike(noteLike);
    }

    /**
     * 批量删除笔记点赞记录
     * 
     * @param likeIds 需要删除的笔记点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteNoteLikeByLikeIds(Long[] likeIds)
    {
        return noteLikeMapper.deleteNoteLikeByLikeIds(likeIds);
    }

    /**
     * 删除笔记点赞记录信息
     * 
     * @param likeId 笔记点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteNoteLikeByLikeId(Long likeId)
    {
        return noteLikeMapper.deleteNoteLikeByLikeId(likeId);
    }

    /**
     * 切换点赞状态
     * 
     * @param userId 用户ID
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @return 结果（true=已点赞，false=已取消点赞）
     */
    @Override
    public boolean toggleLike(Long userId, String noteType, Long noteId)
    {
        NoteLike existLike = noteLikeMapper.selectNoteLikeByUserAndNote(userId, noteType, noteId);
        
        if (existLike != null)
        {
            // 已点赞，取消点赞
            noteLikeMapper.deleteNoteLikeByUserAndNote(userId, noteType, noteId);
            return false;
        }
        else
        {
            // 未点赞，添加点赞
            NoteLike noteLike = new NoteLike();
            noteLike.setUserId(userId);
            noteLike.setNoteType(noteType);
            noteLike.setNoteId(noteId);
            noteLike.setCreateTime(DateUtils.getNowDate());
            noteLikeMapper.insertNoteLike(noteLike);
            return true;
        }
    }

    /**
     * 检查用户是否已点赞
     * 
     * @param userId 用户ID
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @return 结果
     */
    @Override
    public boolean isLiked(Long userId, String noteType, Long noteId)
    {
        NoteLike existLike = noteLikeMapper.selectNoteLikeByUserAndNote(userId, noteType, noteId);
        return existLike != null;
    }

    /**
     * 统计笔记点赞数
     * 
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @return 点赞数
     */
    @Override
    public int countLikesByNote(String noteType, Long noteId)
    {
        return noteLikeMapper.countLikesByNote(noteType, noteId);
    }

    /**
     * 查询用户点赞的笔记列表
     * 
     * @param userId 用户ID
     * @return 点赞记录集合
     */
    @Override
    public List<NoteLike> selectUserLikedNotes(Long userId)
    {
        return noteLikeMapper.selectUserLikedNotes(userId);
    }
}